import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Text Width Example'),
        ),
        body: HomePageWidget(),
      ),
    );
  }
}

class HomePageWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return HomePageWidgetState();
  }

}
class HomePageWidgetState extends State<HomePageWidget> {
  ValueNotifier<double> a = ValueNotifier(0);
  ///此处用GlobalKey符合预期，用LocalKey不行 那此处GlobalKey和LocalKey的区别是什么
  ///GlobalKey：当你需要跨多个Build周期保持对同一个widget的引用时使用。例如，获取widget的尺寸或直接调用其方法。
  ///
  /// LocalKey：在构建列表或网格时，用于确保每个子项可以被唯一标识和正确更新。这有助于提高性能和避免不必要的重建。
  ///
  // 选择使用哪种类型的key取决于你的具体需求：如果你需要跨多个构建周期保持对同一widget的引用，使用GlobalKey；如果你需要唯一标识同一父widget下的子widget，使用LocalKey（如ValueKey, ObjectKey等）。
  GlobalKey key = GlobalKey();
  @override
  void initState() {
    print("wqeqwewqe");
    // TODO: implement initState

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Container(
          color: Colors.blue,
          margin: EdgeInsets.only(left: 10,right: 10),
          child: ValueListenableBuilder(
            valueListenable: a,
            builder: (context,v,child) {
              if(v>1.5){
                return GestureDetector(
                  key: key,
                  behavior: HitTestBehavior.deferToChild,
                  onScaleEnd: (ScaleEndDetails a){
                    print("onPanCancel1");
                  },

                  onScaleStart: (ScaleStartDetails details){
                    print("onPanStart1");

                  },
                  onScaleUpdate: (ScaleUpdateDetails details){
                    print("onPanUpdate12222");
                    print("details.scale2222====${details.scale}");
                    a.value = details.scale;
                  },
                  child: Container(
                    // margin: EdgeInsets.only(top: 30),
                    height: 350,
                    width: 300,
                    color: Colors.red,
                  ),
                );
              }else {
                return Center(
                  child: Stack(
                    clipBehavior: Clip.none,
                    children: [
                      Container(
                        height: 400,
                        width: 400,
                        color: Colors.green,
                      ),
                      Positioned(
                        top: 30,
                        bottom: 0,
                        child: GestureDetector(
                          key: key,
                          behavior: HitTestBehavior.deferToChild,
                          onScaleEnd: (ScaleEndDetails a){
                            print("onPanCancel1");
                          },

                          onScaleStart: (ScaleStartDetails details){
                            print("onPanStart1");

                          },
                          onScaleUpdate: (ScaleUpdateDetails details){
                            print("onPanUpdate1");
                            print("details.scale====${details.scale}");
                            a.value = details.scale;
                          },
                          child: Container(
                            // margin: EdgeInsets.only(top: 30),
                            height: 350,
                            width: 300,
                            color: Colors.red,
                          ),
                        ),
                      ),
                    ],
                  ),
                );
              }
            }
          ),
        ),
      ],
    );
  }
}



